package util;

import static models.ResultNonProb.*;

import java.awt.event.MouseEvent;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;

import org.apache.log4j.Logger;

/**
 * @author Rishi Das Roy
 *
 * @Organization Institute Of Genomics & Integrative Biology
 */
public class ResultTable extends JTable {
	private static Logger log = Logger.getLogger(ResultTable.class);

	private String mName;

	private static Map<String, String> mClasKey = new HashMap<String, String>() {
		{
			put("FDGL", "Fully Dependent on GroEL");
			put("FDGL_VITRO", "Fully Dependent on GroEL(in vitro)");
			put("FDGL_VIVO", "Fully Dependent on GroEL(in vivo)");
			put("PDGL", "Partially Dependent on GroEL");
			put("OIGL", "Only Interacting with GroEL");
			put("NDNI", "Not Dependent Not Interacting with GroEL");

			put(CLASS_III, "Fully Dependent on GroEL");
			put(CLASS_II, "Partially Dependent on GroEL");
			put(CLASS_I, "Only Interacting with GroEL");
			put(NGS, "Not Dependent Not Interacting with GroEL");
		}
	};

	public String getToolTipText(MouseEvent e) {
		String tip = "";
		java.awt.Point p = e.getPoint();
		int rowIndex = rowAtPoint(p);
		int colIndex = columnAtPoint(p);
		int realColumnIndex = convertColumnIndexToModel(colIndex);

		if (realColumnIndex == 0) {
			tip = (String) getValueAt(rowIndex, colIndex);

		} else if (realColumnIndex == 1) { //Veggie column
			String prediction = (String) getValueAt(rowIndex, colIndex);
			if(mClasKey.get(prediction)!=null)
			tip = prediction + ":" + mClasKey.get(prediction);

		} else { //another column
			//You can omit this part if you know you don't
			//have any renderers that supply their own tool
			//tips.
			tip = super.getToolTipText(e);
		}
		return tip;
	}

	public ResultTable() {
		super();
	}

	public ResultTable(int arg0, int arg1) {
		super(arg0, arg1);
	}

	public ResultTable(Object[][] arg0, Object[] arg1) {
		super(arg0, arg1);
	}

	public ResultTable(TableModel arg0, TableColumnModel arg1,
			ListSelectionModel arg2) {
		super(arg0, arg1, arg2);
	}

	public ResultTable(TableModel arg0, TableColumnModel arg1) {
		super(arg0, arg1);
	}

	public ResultTable(TableModel arg0) {
		super(arg0);
	}

	public ResultTable(Vector arg0, Vector arg1) {
		super(arg0, arg1);
	}

	public String getName() {
		return mName;
	}

	public void setName(String name) {
		mName = name;
	}

	public void saveResult(String fastaFilePath) throws IOException {
		TableModel model = this.getModel();
		FileWriter out = new FileWriter(fastaFilePath);
		for (int i = 0; i < model.getColumnCount(); i++) {
			out.write(model.getColumnName(i) + ",");
		}
		out.write(System.getProperty("line.separator"));

		for (int i = 0; i < model.getRowCount(); i++) {
			for (int j = 0; j < model.getColumnCount(); j++) {
				out.write(model.getValueAt(i, j).toString() + ",");
			}
			out.write(System.getProperty("line.separator"));
		}
		out.close();

	}

}
